package testCase;

import com.imagotag.test.LabelIdProvider;
import com.imagotag.test.TestBuilder;
import com.imagotag.test.TestEnvironment;
import com.imagotag.test.results.CsvResultWriter;
import com.imagotag.test.results.TransactionDetailsResultWriter;
import extensions.CSVResultWriterWithEV;
import org.joda.time.DateTime;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

/**
 * @projectName: endurance_test
 * @className: Config
 * @description:
 * @author: JLONG
 * @date: 2024/10/21 16:52
 * @version: 1.0
 */
public class TestConfig {
    // 配置固件信息
    public static String mcu = "pan";
    public static String FW_Version = "3.1.6";
    public static String FW_no = "pvt";

    // 配置测试环境
    public static String encryption = "noPIN";                          // encryption key，默认“noPIN”为非加密状态
    public static String apChannel = "chan_";                           // AP的信道
    public static String serviceUrl = "http://192.168.1.7:8001";        // CoreService地址
    public static String syncProfile = "NORMAL";                        // NORMAL; SUPER_FAST_RESPONSE; ULTRA_FAST_RESPONSE

    // 配置测试标准
    public static double minTemp = 21.50;                               // 采样 最低温限值
    public static double maxTemp = 25.50;                               // 采样 最高温限值
    public static double resistance = 20.00;                            // 采样 内阻限值（新电池）
    public static int refreshDu = 30;                                   // 图片刷新等待时间（秒）

    // 配置测试价签
    public static String[] tags = {"TAG",};                                                         // 筛选符合所有tags的价签
    public static String[] labelsID = {"9CB92993",};                                                // 价签ID数组
    public static String[] labelTypes = {"2.6 BW", "2.6 BWR",};                                     // 价签类型数组
    public static LabelIdProvider labelsWithTags = test -> test.getLabelsWithTags(tags);            // 根据Tae，生成价签对象
    public static LabelIdProvider labelsWithID = test -> test.getLabelsByLabelId(labelsID);         // 根据LabelID，生成价签对象
    public static LabelIdProvider labelsWithType = test -> test.getLabelsOfLabelType(labelTypes);   // 根据LabelType，生成价签对象
    public static LabelIdProvider labelsForRegister = test -> test.getLabelsUnchecked(labelsID);    // 根据LabelID，生成价签对象，用于注册等
    public static LabelIdProvider allLabels = TestEnvironment::getAllLabels;                        // 获取core service中所有价签
    public static LabelIdProvider onlineLabels = TestEnvironment::getOnlineLabels;                  // 获取core service中所有已上线价签

    // 配置测试数据存储路径
    public static String logsPath = "../testResults/";                  // 数据根目录

    // 生成TestBuilder
    public static TestBuilder getTestBuilder(String testType, String transmissionRetriesCount, Boolean  isEncryption) {
        // 配置测试日志
        DateTime now = DateTime.now();
        String datetime = String.format("%d-%02d-%02d_%02d%02d%02d", now.getYearOfCentury(), now.getMonthOfYear(), now.getDayOfMonth(), now.getHourOfDay(), now.getMinuteOfHour(), now.getSecondOfMinute());
        String csvLogName = testType + "_" + TestConfig.mcu + "_" + TestConfig.FW_Version + "_" + TestConfig.FW_no + "_" + TestConfig.apChannel + "_" + datetime+ ".csv";
        String txtLogName = testType + "_" + TestConfig.mcu + "_" + TestConfig.FW_Version + "_" + TestConfig.FW_no + "_" + TestConfig.apChannel + "_" + datetime+ ".txt";
        File logPath = new File(TestConfig.logsPath, testType + "_" + TestConfig.mcu + "_" + TestConfig.FW_Version + "_" + TestConfig.FW_no + "_" + TestConfig.syncProfile + "_" + TestConfig.encryption + "_" + TestConfig.apChannel + "_" + datetime);
        logPath.mkdirs();

        // 创建并配置TestBuilder
        TestBuilder testBuilder = new TestBuilder();
        try {
            testBuilder
                    .writeResults(new CSVResultWriterWithEV(new PrintStream(new File(logPath, csvLogName))))    // 标准csv日志输出
                    .writeResults(new TransactionDetailsResultWriter(new PrintStream(new File(logPath, txtLogName))))   // 标准txt日志输出
                    .dontFailOnExistingSetup()
                    .dontFailOnFirstFailedTest()
                    .iterations(1)
                    .setConfiguration("transmissionRetriesCount", transmissionRetriesCount)  // 任务重试次数，默认3
//                    .disableRetries() // 是否允许任务重试
                    .enableAllTasks()
//                    .enableEncryption("imagotag") //设置 encryption key
                    .disableBackgroundTasks()
                    .setConfiguration("lancomDiscoveryEnabled", "false")
                    .setConfiguration("ignoreArchiveTasksForUpdateStatusQueries", "false")
                    .setConfiguration("syncProfile", TestConfig.syncProfile); // NORMAL（NORMAL; SUPER_FAST_RESPONSE; ULTRA_FAST_RESPONSE）
            if(isEncryption) {
                testBuilder.enableEncryption(TestConfig.encryption); //设置 encryption key
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return testBuilder;
    }
}
